package com.hq.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hq.bean.City;
import com.hq.bean.Course;
import com.hq.bean.Student;
import com.hq.service.StuService;
import com.hq.util.FileUploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class StuController {

    @Resource
    private StuService stuService;


    @RequestMapping("list.do")
    public String list(@RequestParam(value="cpage",defaultValue = "1") Integer cpage,
                       String mohu1, String startTime, String endTime, String sex,
                       Integer sheng, Integer shi, Integer xian, Model model){
        Map map=new HashMap();
        map.put("mohu1",mohu1);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("sex",sex);
        map.put("sheng",sheng);
        map.put("shi",shi);
        map.put("xian",xian);

        // 开始分页
        PageHelper.startPage(cpage,3);
        // 列表查询结果
        List list = stuService.list(map);
        // 将分页的信息和 查询结果 放到PageInfo
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }

    @RequestMapping("cityList.do")
    @ResponseBody
    public Object cityList(Integer pid){
        List<City> list=stuService.cityList(pid);
        return list;
    }
    @RequestMapping("courseList.do")
    @ResponseBody
    public  Object courseList(){
        List<Course> list=stuService.courseList();
        return list;
    }

    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }

    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Student student, @RequestParam("photo") MultipartFile photo, Integer[] cids, HttpSession session){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            student.setPic(upload);
        }
       int i= stuService.add(student,cids);
        return i;
    }

    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer sid){
       int i= stuService.delStu(sid);
        return i;
    }

    @RequestMapping("dels.do")
    @ResponseBody
    public Object dels(String sids){
        int i= stuService.delsStu(sids);
        return i;
    }

    @RequestMapping("toupd.do")
    public String toupd(Integer sid){
        return "upd";
    }

    @RequestMapping("getInfoBySid.do")
    @ResponseBody
    public Object getInfoBySid(Integer sid){
        Map map=stuService.getInfoBySid(sid);
        return map;
    }
    @RequestMapping("upd.do")
    @ResponseBody
    public Object upd(Student student, @RequestParam("photo") MultipartFile photo, Integer[] cids, HttpSession session){
        if(photo.getOriginalFilename().length()>0){
            String upload = FileUploadUtil.load(session, photo, "upload");
            student.setPic(upload);
        }
        int i= stuService.upd(student,cids);
        return i;

    }
}
